#include "luokka.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/statvfs.h>


luokka::luokka()
{

}
void luokka::run(){

    if (volumePoint == ""){
        qDebug() << "volumepoint on tyhjä";
        return;
    }
    qDebug()<<"Testiä" << volumePoint;

    QSettings settings("./theocopy.ini", QSettings::IniFormat);

    QFileInfoList l = scanDir(QDir(settings.value("path", "").toString()));

    bool result = checkSpace(volumePoint);
    qDebug()<< "point " <<volumePoint;
    qDebug()<< "bytes1 "<<bytes;
    qDebug() << "result" << result << "l size" << l.size();
    if(result){
        copyFiles(volumePoint, l);
        qDebug()<<"Menee tänne copsaukseen";
    }
    this->exec();
}
bool luokka::checkSpace(QString volumePoint){

    struct statvfs buffer;
    bytes2 = 0;
    QFileInfo sizeInfo(volumePoint);
    bytes2 = sizeInfo.size();
    QByteArray byteArray = volumePoint.toUtf8();
    const char* cString = byteArray.constData();
    //fildes = open(volumePoint);
    //bytes2 = statvfs(cString, fsblkcnt_t());
    //int stat2 = lstat(cString, off_t());
int status;
status = statvfs(cString, &buffer);

//unsigned long bytes3;
//bytes3 = buffer.f_bavail*buffer.f_frsize;
//qDebug()<<"buffer " << buffer.f_bfree*buffer.f_bsize;
//qDebug()<<"stat "<< status;
unsigned long long dBfree = buffer.f_bfree;
unsigned long long dSize = buffer.f_frsize;
unsigned long long dTotal = dBfree * dSize;
unsigned long long bytes4 = dTotal;

//    qDebug()<<"Bytes 3 tilaa tikulla: "<< bytes3;
//    qDebug()<<"Bytes 1 konventi koko: " << bytes;
    qDebug()<< "tulostaa "<< bytes4;
    if(bytes <= bytes4){
        qDebug()<< "Tilaa on";

        return true;
    }else{
        emit filescopied(0,0,false);
        qDebug()<< "Tilaa ei ole";
        return false;

    }
}
void luokka::copyFiles(QString volumePoint,QFileInfoList list){

    int count = list.count();
    qDebug() << "kopioitavia tiedostoja:" <<list.size();
    QString str = QString();
    emit filescopied(list.count(),0,true);

    for(int i=0;i<count;i++)
    {
        QFileInfo info = list.at(i);
        str = info.filePath();
        bool copied = CopyFile(str, volumePoint);
        qDebug() << "kopiointi " << str << copied;
        emit filescopied(list.count(),i+1,true);

        //Code
    }
}

bool luokka::CopyFile(const QString& sourceFile, const QString& destinationDir)
{
    QFileInfo fileInfo(sourceFile);
    QString destinationFile = destinationDir + QDir::separator() + fileInfo.fileName();
    qDebug() << "from:" << sourceFile << "to:" << destinationFile;
    bool result = QFile::copy(sourceFile, destinationFile);
    //bool result;
    //QFile file(sourceFile);
    //file.copy(destinationFile);

    return result;
}
QFileInfoList luokka::scanDir(QDir dir){
    dir.setNameFilters(QStringList("*.mp3"));
    QFileInfoList list = dir.entryInfoList();
    QFileInfoList newlist;
    qDebug()<< "List "<< list.size();
    bytes = 0;
    qDebug()<<"Päivä: "<<paiva1;
    for (int i = 0; i < list.size(); ++i){
        QFileInfo fileInfo = list.at(i);

        if(paiva1 && fileInfo.lastModified().date() == QDate::currentDate()){
            qDebug() << "yksi päivä";
            newlist.append(fileInfo);
        }else if(paiva2 && fileInfo.lastModified().date() == QDate::currentDate() ||
                            fileInfo.lastModified().date() == QDate::currentDate().addDays(-1)){
            qDebug() <<"kaksi päviää";
            newlist.append(fileInfo);
        }else{
            qDebug() << "ehdot ei toteudu";
            continue;
        }
        qDebug() << qPrintable(QString("%1 %2").arg(fileInfo.size(), 10).arg(fileInfo.fileName()));
        bytes += fileInfo.size();
        qDebug() << "Bytes in folder loop: " <<bytes;

    }
    qDebug() << "Bytes in folder: " <<bytes;

    return newlist;
}
